/*
 * @description: 
 * @Author: wzg
 * @Date: 2021-03-18 16:38:06
 * @LastEditTime: 2021-03-19 13:59:13
 */
/* 自动扫描并导入所有Model */
import { readdirSync } from 'fs';
import { resolve } from 'path';
import { fileURLToPath } from 'node:url';
import { dirname } from 'node:path';
import db from './db.js';

/* 获取当前路径的上一级路径 */
const upperLevelPath = resolve(dirname(fileURLToPath(import.meta.url)), '..');
let files = readdirSync(upperLevelPath + '/models');
/* 过滤得到以 .js结尾的文件*/
let js_files = files.filter((file) => {
  return file.endsWith('.js');
});
console.log(js_files);
let model = {};
for (let file of js_files) {
  console.log(`import model from file ${file}...`);
  /* 获取文件名 */
  let name = file.substring(0, file.length - 3);
  const { default: moduleA } = await import(upperLevelPath + '/models/' + file);
  model[name] = moduleA;
}
console.log(model);
model.sync = () => {
  db.sync();
}
export default model;

// export function sync () {
//   db.sync();
// }